package com.esteel.settlement.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.esteel.settlement.config.pa.PaTemplate;
import com.esteel.settlement.config.pa.PacketFactory;
import com.esteel.settlement.dao.SettFundRepository;
import com.esteel.settlement.entity.TbSettFund;
import com.esteel.settlement.entity.TbSettFundPK;
import com.esteel.settlement.pa.packet.Answer;
import com.esteel.settlement.pa.packet.CheckBalanceOfBankAccountPB1010;
import com.esteel.settlement.pa.packet.Query1010Array;
import com.esteel.settlement.pa.packet.RspBP1010;
import com.esteel.settlement.service.SettFundService;
import com.esteel.settlement.vo.FundQueryVo;
import com.esteel.settlement.vo.TbSettFundVo;

@Service
public class SettFundServiceImpl implements SettFundService{

	@Autowired
	private SettFundRepository settFundRepository;
	
	@Autowired
	private PaTemplate paTemplate;
	
	@Autowired
	private PacketFactory packetFactory;
	
	@Override
	public TbSettFund save(TbSettFund entity) {
		return settFundRepository.save(entity);

	}

	
	@Override
	public List<TbSettFund> selectFund(TbSettFund entity) {
		Example<TbSettFund> e= Example.of(entity);
		return settFundRepository.findAll(e);
	}

	@Override
	public TbSettFund findOne(TbSettFundPK pk) {
		return settFundRepository.findOne(pk);
	}

	@Override
	public TbSettFund findCharges() {
		return settFundRepository.findFirstByCustType(3);
	}

	@Override
	public TbSettFund findAccrual() {
		return settFundRepository.findFirstByCustType(4);
	}

	@Override
	public TbSettFund findSettlement() {
		return settFundRepository.findFirstByCustType(6);
	}


	@Override
	public TbSettFund findByCustAcctId(String custAcctId) {
		return settFundRepository.findByCustAcctId(custAcctId);
	}


	@Override
	public Answer syncFundInfo(String custAcctId) {
		
		CheckBalanceOfBankAccountPB1010 pb1010 = packetFactory.getPacket(CheckBalanceOfBankAccountPB1010.class);
		pb1010.setSelectFlag(1);
		pb1010.setPageNum(1);
		pb1010.setCustAcctId(custAcctId);
		
		pb1010.setThirdLogNo("o" + new Date().getTime());
		RspBP1010 rsp = paTemplate.execute(pb1010, RspBP1010.class);
		
		if (rsp.isSuccess()){
			List<Query1010Array> list = rsp.getQuery1010Array();
			for (Query1010Array obj:list){
				TbSettFundPK pk = new TbSettFundPK();
				pk.setCustAcctId(obj.getCustAcctId());
				pk.setThirdCustId(obj.getThirdCustId());
				TbSettFund fund = this.findOne(pk);
				if (fund==null){
					fund = new TbSettFund();
					fund.setId(pk);
				}
				
				BeanUtils.copyProperties(obj, fund);
				fund.setSupAcctId(pb1010.getSupAcctId());
				fund.setSettDate(new Date());
				this.save(fund);
				
			}
		}

		return rsp;
	}


	@Override
	public Page<TbSettFundVo> findAll(FundQueryVo vo) {
		return settFundRepository.findAll(vo);
	}


}
